$(function () {
    //工具栏快速搜索框事件
    $("#zjk-salePlan-search").searchbox({
        searcher: function () {
            //获取快速搜索的类型
            var searchName = $('#zjk-salePlan-search').searchbox('getName');
            //获取搜索框的值
            var searchVal = $('#zjk-salePlan-search').searchbox('getValue');
            if (searchName == 'planCheck') {
                clearDataGrid();
                $('#zjk-salePlan-dg').datagrid({
                    queryParams: null
                });
                $('#zjk-salePlan-dg').datagrid({
                    url: 'find-all-salePlan',
                    method: 'post',
                    queryParams: {
                        planCheck: searchVal
                    }
                });

                $('#zjk-salePlan-dg').datagrid('reload');
            } else if (searchName == 'planName') {
                clearDataGrid();
                $('#zjk-salePlan-dg').datagrid({
                    queryParams: null
                });
                $('#zjk-salePlan-dg').datagrid({
                    url: 'find-all-salePlan',
                    method: 'post',
                    queryParams: {
                        planName: searchVal
                    }
                });

                $('#zjk-salePlan-dg').datagrid('reload');

            }


        }

    });

    //点击工具栏的“查询所有”按钮，查询所有销售计划
    $("#zjk-search-all-salePlan-btn").linkbutton({
        onClick: function () {
            $('#zjk-salePlan-dg').datagrid({
                queryParams: null
            });
            $('#zjk-salePlan-dg').datagrid({
                url: 'find-all-salePlan',
                method: 'post',
                queryParams: {
                    stage: null
                },
            });
            $('#zjk-salePlan-dg').datagrid('reload');
            $("#zjk-salePlan-search").searchbox('setValue', null);
        }
    });

    //点击计划主表中一行，下方从表显示对应计划明细
    $("#zjk-salePlan-dg").datagrid({
        onClickCell: function (index) {
            var rows = $('#zjk-salePlan-dg').datagrid('getRows');
            var id = rows[index].planId;
            $('#zjk-salePlan-edtalis-dg').datagrid({
                url: 'getSalePlanDetailsList',
                queryParams: {
                    planId: rows[index].planId
                },
                method: 'post'
            });
        }
    });

    //工具栏删除按钮事件
    $("#delete-salePlan").linkbutton({
        onClick: function () {
            var rows = $('#zjk-salePlan-dg').datagrid('getChecked');
            if (rows.length == 0) {
                $.messager.alert("提示", "请先选择要删除的行！");
            } else {
                $.messager.confirm("询问", "您确定要删除吗？", function (v) {
                    if (v) {
                        for (var i = 0; i < rows.length; i++) {
                            if (rows[i]) {
                                var id = rows[i].planId;
                                $.post('deleteSalePlan', {planId: id}, function (r) {
                                    if (r.success) {
                                        $.messager.alert("提示", "恭喜，删除成功！");

                                        $('#zjk-salePlan-dg').datagrid('reload');
                                    } else {
                                        $.messager.alert("提示", r.messager);
                                    }
                                });
                            }
                        }
                    }
                })
            }
        }
    });
    //操作栏事件处理
    $("#zjk-salePlan-dg").datagrid({
        onLoadSuccess: function (data) {
            //视图
            $("a[name='details']").linkbutton({
                width: 40, iconCls: 'icon-my_detail', plain: true
            });
            //操作栏删除事件
            $("a[name='delete']").linkbutton({
                width: 40, iconCls: 'icon-my_delete', plain: true,
                onClick: function () {
                    $('#zjk-salePlan-dg').datagrid({
                        onClickCell: function (index) {
                            var rows = $('#zjk-salePlan-dg').datagrid('getRows');
                            var planId = rows[index].planId;
                            $.post('deleteSalePlan', {planId: planId}, function (r) {
                                if (r.success) {
                                    $.messager.alert("提示", "恭喜，删除成功！");
                                    $('#zjk-salePlan-dg').datagrid('reload');
                                } else {
                                    $.messager.alert("提示", r.message);
                                }
                            });
                            $('#zjk-salePlan-dg').datagrid({
                                onClickCell: function (index) {
                                }
                            });
                        }

                    });


                }
            });
            //编辑
            $("a[name='edit']").linkbutton({
                width: 40, iconCls: 'icon-my_edit', plain: true,
                onClick: function () {
                    $('#zjk-salePlan-dg').datagrid({
                        onClickCell: function (index) {
                            var rows = $('#zjk-salePlan-dg').datagrid('getRows');
                            var row = rows[index];// 根据index获得其中一行。
                            if (row) {
                                $('#zjk-edit-salePlan-and-planDetails').dialog({
                                    onLoad: function () {
                                        $("#zjk-salePlan-and-planDetails-f-view").form('load', row);
                                        $('#zjk-planDetails-dg-view').datagrid({
                                            url: 'getSalePlanDetailsList',
                                            queryParams: {
                                                planId: rows[index].planId
                                            },
                                            method: 'post'
                                        });

                                    }

                                });
                                //加载完成后打开窗口
                                $('#zjk-edit-salePlan-and-planDetails').dialog('open')
                            }
                        }
                    });

                }


            });

        }
    });

    //编辑窗口中的最大窗口的“关闭”按钮事件
    $("#zjk-edit-salePlan-and-planDetails-bb-close").linkbutton({
        onClick: function () {
            $('#zjk-edit-salePlan-and-planDetails').dialog('close');
        }
    })

    //编辑窗口中的最大窗口的“保存”按钮事件
    $("#zjk-edit-salePlan-and-planDetails-bb-save").linkbutton({
        onClick: function () {
            $('#zjk-salePlan-and-planDetails-f-view').form({
                onSubmit: function (parmps) {
                    //进行验证
                    return $(this).form('validate');

                },
                success: function (r) {
                    r = eval('(' + r + ')');
                    if (r.success) {
                        //点击关闭，则窗口关闭
                        $.messager.alert('成功', '恭喜，修改成功！');
                        $('#zjk-edit-salePlan-and-planDetails').dialog('close');
                        $('#zjk-salePlan-dg').datagrid('reload');
                    } else {
                        $.messager.alert("错误", r.messager);
                    }
                }
            }).submit();

        }
    });
    //打开编辑中的"添加"明细窗口
    $('#edit-zjk-form-salePlan-edtails-tools-add').linkbutton({
        onClick: function () {
            $('#edit-add-salePlan-details').dialog({
                onLoad: function () {
                    $('#edit-zhong-clientId').combobox({
                        url: 'saling/getClientInfoList',
                        method: 'post',
                        panelHeight: 'auto',
                        valueField: 'cid',
                        textField: 'cname'

                    });
                    $("#edit-zhong-userId").combobox({
                        url: 'saling/getUserInfoList',
                        method: 'post',
                        panelHeight: 'auto',
                        valueField: 'uId',
                        textField: 'uname'
                    });

                }
            });
            $('#edit-add-salePlan-details').dialog('open');

        }
    });

    //编辑窗口的添加关闭按钮事件
    $("#edit-zjk-salePlan-edtails-add-dialog-bb-close").linkbutton({
        onClick: function () {
            $('#edit-add-salePlan-details').dialog('close');
        }
    })
    //编辑窗口的添加保存按钮事件
    $("#edit-zjk-salePlan-edtails-add-dialog-bb-save").linkbutton({
        onClick: function () {
            $('#zjk-eait-and-add-one-planDetails-f').form({
                url: 'add-planDetails',
                method: 'get',
                onSubmit: function () {
                    return $(this).form('validate')
                },
                success: function (data) {
                    $('#edit-add-salePlan-details').dialog('close');
                    $('#zjk-planDetails-dg-view').datagrid('reload');
                    //刷新展示列表
                    $('#zjk-salePlan-dg').datagrid('reload');
                },
            }).submit();

        }
    });

    //编辑窗口中的“删除”按钮事件
    $("#edit-zjk-form-salePlan-edtails-remove").linkbutton({
        onClick: function () {
            var row = $("#zjk-planDetails-dg-view").datagrid("getSelected")
            if (row != null) {
                var index = $("#zjk-planDetails-dg-view").datagrid('getRowIndex', row);
                $("#zjk-planDetails-dg-view").datagrid('deleteRow', index)
                rows = JSON.stringify(row)
                $.post("zjk-deletePlanDetails", {
                        rows: rows,
                        saleDetId: row.planDetail.salePlan.planId
                    },
                    function (result) {
                        if (result.success == true) {
                            $.messager.alert("提示", "删除成功")
                        } else {
                            $.messager.alert("提示", result.messager)

                        }
                    }
                )

            } else {
                $.messager.alert("提示", "请先选择要删除的行！")
            }
        }
    });


    //编辑窗口中的“修改”按钮事件
    $("#edit-zjk-form-salePlan-edtails-edit").linkbutton({
        onClick: function () {
            var row = $("#zjk-planDetails-dg-view").datagrid('getSelected')
            if (row) {
                $("#chun-edit-salePlan-details").dialog({
                    onLoad: function () {
                        $.post('getOnePlanDetails', {saleDetId: row.planDetail.saleDetId}, function (r) {
                            if (r) {
                                $('#chun-edit-pname').textbox({value: r.productionDetail.pname})
                                $('#chun-edit-pid').textbox({value: r.productionDetail.pid})
                                $('#chun-edit-pwunit').textbox({value: r.productionDetail.pwunit})
                                $('#chun-edit-poprice').textbox({value: r.productionDetail.poprice})
                                $('#chun-edit-planTime').datebox({value: r.planDetail.planTime})
                                $('#chun-edit-expectGain').textbox({value: r.planDetail.expectGain})
                                $('#chun-edit-planMoney').textbox({value: r.planDetail.planMoney})
                                $('#chun-edit-clientId').textbox({value: r.cname})
                                $('#chun-edit-userId123').textbox({value: r.uname})
                                $('#chun-edit-saleDetId').val(r.planDetail.saleDetId)

                            }

                        })

                    },
                });
                $("#chun-edit-zjk-salePlan-edtails-add-dialog-bb-save").linkbutton({
                    onClick: function () {
                        $('#chun-edit-planDetails-f').form({
                            url: 'editPlanDetails',
                            method: 'post',
                            onSubmit: function () {
                                var isValid = $(this).form('validate');
                                return isValid;
                            },
                            success: function (data) {
                                $('#chun-edit-salePlan-details').window('close');
                                $('#zjk-planDetails-dg-view').datagrid('reload');
                                $('#zjk-salePlan-dg').datagrid('reload');

                            }
                        }).submit();
                    }
                })
                $("#chun-edit-salePlan-details").window('open')
            } else {
                $.messager.alert('提示', "请先选中一行！！！")
            }


        }
    });


    //添加一个销售计划
    //打开添加销售计划对话框
    $("#zjk-add-salePlan-btn").linkbutton({
        onClick: function () {
            $('#zjk-salePlan-add-dialog').dialog('open');
        }
    });
    //关闭按钮事件
    $("#zjk-salePlan-add-dialog-bb-close").linkbutton({
        onClick: function () {
            $('#zjk-salePlan-add-dialog').dialog('close');
        }
    });
    //保存按钮事件
    $("#zjk-salePlan-add-dialog-bb-save").linkbutton({
        onClick: function () {
            //提交表单事件
            $('#zjk-salePlan-f').form({
                onSubmit: function (parmps) {
                    //表单验证
                    return $(this).form('validate');
                },
                success: function (r) {
                    if (r != null) {
                        $.messager.alert("提示", "恭喜，添加成功！您可在操作栏再次编辑计划及计划明细。");

                        //刷新展示列表
                        $('#zjk-salePlan-dg').datagrid('reload');
                        //关闭窗口
                        $('#zjk-salePlan-add-dialog').dialog('close');
                        //清空表单
                        $('#zjk-salePlan-f').form('clear');


                    } else {
                        $.messager.alert("错误", r.messager);
                    }

                }
            }).submit();


        }
    });
    //点击“编辑明细”按钮，打开销售计划视图窗口
    $("#zjk-salePlan-add-and-edit-bb-go").linkbutton({
        onClick: function () {
            $('#zjk-salePlan-f').form({
                onSubmit: function (parmps) {
                    //表单验证
                    return $(this).form('validate');
                },
                success: function (r) {
                    if (r != null) {
                        // alert(r)
                        //刷新展示列表
                        $('#zjk-salePlan-dg').datagrid('reload');
                        $('#zjk-salePlan-add-plan-Details').dialog({
                            onLoad: function () {
                                $.post('findSalePlanById', {id: r}, function (r) {
                                    $('#planId-view').textbox({value: r.planId});
                                    $('#planName-view').textbox({value: r.planName});
                                    $('#userId-view').combobox({value: r.userId});
                                    $('#planCheck-view').combobox({value: r.planCheck});
                                    $('#strategyProduct-view').textbox({value: r.strategyProduct});
                                    $('#strategyPrice-view').textbox({value: r.strategyPrice});
                                    $('#expenseBudget-view').textbox({value: r.expenseBudget});
                                    $('#setTime-view').textbox({value: r.setTime});
                                    $('#updateDate-view').textbox({value: r.updateDate});
                                });

                            }
                        });
                        $('#zjk-salePlan-add-plan-Details').dialog('open');
                        //关闭窗口
                        $('#zjk-salePlan-add-dialog').dialog('close');
                        //清空表单
                        $('#zjk-salePlan-f').form('clear');
                    } else {
                        $.messager.alert("提示", "您还没有填写计划！")
                    }
                }
            }).submit();

        },


    });


    //打开添加计划销售的商品对话框
    $("#zjk-form-salePlan-edtails-tools-add").linkbutton({
        onClick: function () {

            $('#add-salePlan-details').dialog({
                onLoad: function () {
                    $('#clientId').combobox({
                        url: 'saling/getClientInfoList',
                        method: 'post',
                        panelHeight: 'auto',
                        valueField: 'cid',
                        textField: 'cname'

                    });
                    $("#userId123").combobox({
                        url: 'saling/getUserInfoList',
                        method: 'post',
                        panelHeight: 'auto',
                        valueField: 'uId',
                        textField: 'uname'
                    });

                }
            });
            $('#add-salePlan-details').dialog('open');
        }
    });
    //明细关闭按钮事件
    $("#zjk-salePlan-edtails-add-dialog-bb-close").linkbutton({
        onClick: function () {
            $('#add-salePlan-details').window('close');
        }
    });
    //明细保存按钮事件
    $("#zjk-salePlan-edtails-add-dialog-bb-save").linkbutton({
        onClick: function () {


            $('#zjk-add-one-planDetails-f').form({
                url: 'add-planDetails',
                method: 'get',
                onSubmit: function () {
                    return $(this).form('validate')
                },
                success: function (data) {

                    var row = {
                        "productionDetail": {
                            pId: $('#salePlanId').textbox('getValue'),
                            pname: $('#pname').combobox('getText'),
                            pwunit: $('#pwunit').textbox('getValue'),
                            poprice: $('#poprice').textbox('getValue')
                        },
                        "planDetail": {
                            expectGain: $('#expectGain').textbox('getValue'),
                            planMoney: $('#planMoney').textbox('getValue'),
                            planTime: $('#planTime').datebox('getValue'),
                            clientId: $('#clientId').combobox('getText'),
                            userId123: $('#userId123').combobox('getText')
                        },


                    }
                    $('#zjk-add-salePlan-edtalis-dg').datagrid('appendRow', row);

                    $('#add-salePlan-details').dialog('close');
                    //刷新展示明细的列表
                    $('#zjk-add-salePlan-edtalis-dg').datagrid('reload');
                    //刷新展示主从列表
                    $('#zjk-salePlan-dg').datagrid('reload');


                }
            }).submit();


        }
    });


    //视图窗口保存按钮事件
    $("#zjk-salePlan-add-planDetails-bb-save").linkbutton({
        onClick: function () {

            $('#zjk-salePlan-f-view').form({
                onSubmit: function (parmps) {
                    //进行验证
                    return $(this).form('validate');

                },
                success: function (r) {
                    r = eval('(' + r + ')');
                    if (r.success) {
                        //点击关闭，则窗口关闭
                        $.messager.alert('成功', '恭喜，编辑成功！');
                        $('#zjk-salePlan-add-plan-Details').dialog('close');
                        $('#zjk-salePlan-dg').datagrid('reload');
                    } else {
                        $.messager.alert("错误", r.messager);
                    }
                }
            }).submit();

        }
    })
    //视图窗口关闭按钮事件
    $("#zjk-salePlan-add-planDetails-bb-close").linkbutton({
        onClick: function () {
            $('#zjk-salePlan-add-plan-Details').dialog('close');
        }
    })

})

// 动态添加操作栏图标
function format(value, row, index) {
    var str = /*'<a name="details" href="#" class="easyui-linkbutton edit-btn" data-options="" title="视图"></a>' +*/
        '<a name="delete" href="#" class="easyui-linkbutton edit-btn" title="删除"></a>' +
        '<a name="edit" href="#" class="easyui-linkbutton edit-btn" title="编辑"></a>';
    return str;

}

/* 清除数据表格中的数据 */
function clearDataGrid() {
    // 获取当前页的记录数
    var item = $('#zjk-salePlan-dg').datagrid('getRows');
    if (item != null) {
        for (var i = item.length - 1; i >= 0; i--) {
            var index = $('#zjk-salePlan-dg').datagrid('getRowIndex', item[i]);
            $('#zjk-salePlan-dg').datagrid('deleteRow', index);
        }
    }

}

function formatM(value, row, index) {
    if (value != null) {
        var y = String(value).indexOf(".") + 1;//获取小数点的位置
        var count = String(value).length - y;//获取小数点后的个数
        if (y > 0 && count == 1) {
            return '<b style="color: #9cc8f7;">' + "￥" + value + "0" + '</b>';
        } else if (y > 0 && count > 1) {
            return '<b style="color: #9cc8f7;">' + "￥" + value + '</b>';
        } else {
            return '<b style="color: #9cc8f7;">' + "￥" + value + ".00" + '</b>';
        }
    } else {
        return value;
    }
}